<template>
	<view class="content" :style="{'padding-top':top+'px','min-height':windowheight+'px'}">
		<Navbar title="可用优惠券" bgColor="#da708b" :backIcon="false" style="color: #ffffff;z-index: 1;"></Navbar>
		<view v-if="coupons.length===0" style="position: absolute;top: 50%;left: 50%;transform: translate(-50%,-50%);">暂无优惠券</view>
		<radio-group @change="change" v-if="isThereACoupon===true">
			<view class="coupon_item" v-for="(item,index) in coupons" :key="index">
				<view class="couponi_tit">
					<view class="couponit_amount" v-if="item.couponType ==='FAVORABLE'">￥{{item.couponFavorable}}</view>
					<view class="couponit_amount" v-else>{{item.couponDiscount*10}}折</view>
					<view class="couponit_condt">满{{item.amountFull}}元可用</view>
				</view>
				<view class="couponi_con">
					<view class="couponic_txt">
						<text class="couponict_tit">{{item.couponRuleName}}</text>
						<text class="couponict_mer">仅限在{{item.remark}}满足条件时使用</text>
						<uni-dateformat :date="item.expirationTime" format="有效期至yyyy-MM-dd"
							class="couponict_exp"></uni-dateformat>
					</view>
				</view>
				<radio class="radio" :value="index"></radio>
			</view>
		</radio-group>
		<view v-if="isThereACoupon===false" class="noCoupon">
			暂无优惠券
		</view>
		<view class="button" :style="{position:position}" v-if="isThereACoupon===true">
			<button @click="determine" v-if="isThereACoupon===true">确定</button>
		</view>
	</view>
</template>
<script setup>
	import { ref,onMounted } from 'vue'
	import {
		onShow,
		onLoad
	} from '@dcloudio/uni-app'
	const baseUrl = getApp().globalData.baseUrl
	const title=ref('Hello')
	const windowheight=ref()
	const platform = ref()
	const statusBar = ref()
	const customBar = ref()
	const top = ref()
	const height = ref()
	const position = ref()
	const index = ref()
	const photographerList = ref([])
	const isThereACoupon = ref()
	const coupons = ref([])
	const orderAmount = ref()
	const change = (e) => {
		index.value = e.detail.value
		console.log(e.detail.value, index.value)
	}
	const determine = () => {
		console.log(index.value)
		uni.navigateBack({
			delta: 1
		})
		uni.$emit('backWithData', {
			index: index.value
		});
	}
	onLoad((opt) => {
		console.log(opt)
		orderAmount.value = opt.orderAmount
	})
	const getColumnHeight=()=>{
		uni.getSystemInfo({
			success(res) {
				console.log(res)
				platform.value = res.platform
				windowheight.value=res.screenHeight
				// #ifdef MP-WEIXIN
				statusBar.value = res.statusBarHeight
				const custom = wx.getMenuButtonBoundingClientRect()
				customBar.value = custom.bottom + custom.top - res.statusBarHeight
				// #endif
				// #ifdef APP-PLUS
				statusBar.value = res.statusBarHeight
				customBar.value = res.statusBarHeight + 45
				// 这里是在安卓手机上加上 3 像素（当时好像是在安卓水滴屏上，系统导航栏高度较低才加上去的，大家可以真机自己调试一下）
				if (res.platform == "android") {
					statusBar.value += 3
					customBar.value += 3
				}
				// #endif
			}
		})
		top.value = customBar.value
		windowheight.value=windowheight.value-top.value
		//console.log(statusBar.value, customBar.value, top.value,windowheight.value)
	}
	onShow(()=>{
		coupons.value === []
		const now = new Date().getTime()
		if (!uni.getStorageSync('token') || now > uni.getStorageSync('expireTime')) {
			uni.reLaunch({
				url: "/pages/login/login"
			})
		}else{
			uni.request({
				url: getApp().globalData.baseUrl + "/coupon/list",
				method: "POST",
				header: {
					'content-type': 'application/json',
					'Access-Control-Allow-Origin': "*",
					'Authorization': uni.getStorageSync('token')
				},
				data: {
					clientNo: JSON.parse(uni.getStorageSync("user_info")).client_no,
					couponStatus:"UNUSED"
			
				},
				success: (res) => {
					console.log(res)
					if (res.data.value.length === 0) {
						isThereACoupon.value = false
					} else {
						isThereACoupon.value = true
						for (let i = 0; i < res.data.value.length; i++) {
							if (res.data.value[i].amountFull <= orderAmount.value) {
								coupons.value.push(res.data.value[i])
							}
						}
						//coupons.value = res.data.value
						if (coupons.value.length < 9) {
							position.value = "absolute"
						} else {
							position.value = "sticky"
						}
					}
				},
				fail: (err) => {
					console.log(err)
				}
			})
		}
	})
	onMounted(()=>{
		getColumnHeight()
	})
</script>
<style lang="less">
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		background-image: linear-gradient(to bottom,#d95e7d,#ffffff);
	}
	.button {
		height: 110rpx;
		bottom: 0;
		width: 100%;
	
		button {
			background-color: #e77c90;
			color: #fff;
			border-radius: 0rpx;
			width: 100%;
			height: 110rpx;
			line-height: 110rpx;
		}
	}
	
	.coupon_item {
		border: 1px solid #ddd;
		height: 150rpx;
		margin-bottom: 10rpx;
		margin-left: 10rpx;
		margin-right: 10rpx;
		border-radius: 8rpx;
		display: flex;
		position: relative;
	
		.radio {
			position: absolute;
			right: 20rpx;
			top: 50%;
			transform: translate(0, -50%);
		}
	}
	
	.couponi_tit {
		background-color: #fee3e4;
		width: 200rpx;
		padding: 20rpx;
		text-align: center;
		color: #e77c90;
	}
	
	.couponi_con {
		background-color: #fff;
		padding: 20rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		width: 100%;
	}
	
	.couponit_amount {
		font-size: 32rpx;
		font-weight: bold;
		margin: 10rpx 0;
	}
	
	.couponit_condt {
		font-size: 24rpx;
	}
	
	.couponic_txt {
		font-size: 24rpx;
		display: flex;
		flex-direction: column;
		line-height: 40rpx;
	
	}
	
	.couponict_tit {
		font-size: 30rpx;
	}
	
	.couponict_mer,
	.couponict_exp {
		color: #ccc;
	}
	
	.noCoupon {
		position: absolute;
		text-align: center;
		width: 100%;
		top: 45%;
		font-size: 45rpx;
	}
</style>